package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moji.common.base.ApiResult;
import com.moji.entity.RoleMenuRel;
import com.moji.entity.SysUser;
import com.moji.entity.UserInfo;
import com.moji.entity.dto.ReqLoginDto;
import com.moji.entity.dto.RespLoginDto;
import com.moji.entity.dto.TreeMenuDto;
import com.moji.mapper.SysUserMapper;
import com.moji.service.RoleMenuRelService;
import com.moji.service.SystemService;
import com.moji.util.JwtTokenUtil;
import com.moji.util.MD5Util;
import com.moji.util.ResultCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Service
public class SystemServiceImpl implements SystemService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private RedisTemplate redisTemplate;


    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public ApiResult login(ReqLoginDto loginReqDao) {
        String loginId = loginReqDao.getLoginId();
        String password = loginReqDao.getPassword();
        //校验登陆账号不为空
        if(StringUtils.isEmpty(loginId)){
            return ApiResult.failed(ResultCode.PARAMS_IS_NULL.getMsg());
        }
        //校验登陆密码不为空
        if(StringUtils.isEmpty(password)){
            return ApiResult.failed(ResultCode.PARAMS_IS_NULL.getMsg());
        }

        String md5Password = MD5Util.getMd5(password).toUpperCase();
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SysUser::getLoginId,loginId).eq(SysUser::getPassword,md5Password);
        SysUser sysUser = sysUserMapper.selectOne(wrapper);
        if(sysUser == null){
            throw new RuntimeException("用户不存在,请核查!");
        }
        //jwt生成token
        UserInfo userInfo = new UserInfo();
        userInfo.setLoginId(sysUser.getLoginId());
        userInfo.setUserCode(sysUser.getUserCode());
        userInfo.setUserName(sysUser.getUserName());
        userInfo.setDeptCode(sysUser.getDeptCode());
        userInfo.setMobile(sysUser.getMobile());
        userInfo.setRoleCode(sysUser.getRolesCode());
        String token = jwtTokenUtil.createToken(userInfo);
        //放入redis缓存
//        if(redisTemplate.hasKey(token)){
//            redisTemplate.delete(token);
//            redisTemplate.opsForValue().set(token,token, Integer.valueOf(MdsEnumConfig.CCP_SYSTEM_TOKEN_TIME.getCode()), TimeUnit.MINUTES);
//        }else{
//            redisTemplate.opsForValue().set(token,token, Integer.valueOf(MdsEnumConfig.CCP_SYSTEM_TOKEN_TIME.getCode()), TimeUnit.MINUTES);
//        }

        //返给前端登录人基本信息和权限
        RespLoginDto respDao = new RespLoginDto();
        respDao.setId(sysUser.getId());
        respDao.setUserCode(sysUser.getUserCode());
        respDao.setUserName(sysUser.getUserName());
        respDao.setLoginId(sysUser.getLoginId());
        respDao.setRolesCode(sysUser.getRolesCode());
        respDao.setRolesName(sysUser.getRolesName());
        respDao.setDeptCode(sysUser.getDeptCode());
        respDao.setDeptName(sysUser.getDeptName());
        respDao.setEmail(sysUser.getEmail());
        respDao.setMobile(sysUser.getMobile());
        respDao.setBirthday(sysUser.getBirthday());
        respDao.setSex(sysUser.getSex());
        respDao.setAuthorization(token);

        return ApiResult.ok(respDao);
    }

    @Override
    public void logout(String loginId, HttpServletRequest request) {
        //String key = Base64Util.encrypt(loginId);
        String token = request.getHeader("authorization");
        if(redisTemplate.hasKey(token)){
            redisTemplate.delete(token);
        }
    }
}
